import turtle
import time
import datetime

print("迷宫游戏, 请使用方向键控制小乌龟移动")
t0 = time.time()

# 创建一个窗口
wn = turtle.Screen()
# 设置窗口标题
wn.title("迷宫游戏")
# 设置窗口背景色
wn.bgcolor("white")
# 设置画布大小
wn.setup(800, 500)
# 创建一个小乌龟
player = turtle.Turtle()
# 设置小乌龟的颜色为绿色
player.color("green")
# 设置小乌龟的形状为乌龟
player.shape("turtle")
# 将小乌龟的画笔提起，避免在移动时留下痕迹
player.penup()

# 创建迷宫的墙
walls = [
    (0, 200, 400, 10),
    (0, -200, 400, 10),
    (200, 0, 10, 400),
    (-200, 0, 10, 400),
    (-100, 150, 10, 100),
    (-100, -150, 10, 100),
    (100, 50, 10, 100),
    (100, -50, 10, 100),
    (50, 100, 100, 10),
    (-150, 100, 100, 10),
    (-50, 0, 100, 10),
    (-150, -100, 100, 10),
    (-50, -100, 100, 10),
    (0, 50, 100, 10),
    (0, -50, 100, 10),
    (-200, 150, 10, 50),
    (200, -150, 10, 50),
    (150, 100, 50, 10),
    (-200, -150, 50, 10),
    (-100, 50, 10, 50),
    (100, -100, 10, 50),
    (-150, -50, 50, 10),
    (50, -50, 100, 10)
]

# 在迷宫中画出墙
print("开始画墙")
for wall in walls:
    x, y, width, height = wall
    # 创建一个画笔来画墙
    wall_turtle = turtle.Turtle()
    # 隐藏画笔形状
    wall_turtle.hideturtle()
    # 设置画笔速度为最快
    wall_turtle.speed(0)
    # 设置画笔颜色为灰色
    wall_turtle.color("#333")
    # 提起画笔
    wall_turtle.penup()
    # 将画笔移动到墙的左上角位置
    wall_turtle.goto(x, y)
    # 放下画笔，准备开始画墙
    wall_turtle.pendown()

    # 开始填充墙的颜色
    wall_turtle.begin_fill()

    # 循环两次，画出墙的四面
    for i in range(2):
        # 向前移动width的距离
        wall_turtle.forward(width)
        # 左转90度
        wall_turtle.left(90)
        # 向前移动height的距离
        wall_turtle.forward(height)
        # 左转90度
        wall_turtle.left(90)

    # 结束填充墙的颜色
    wall_turtle.end_fill()

    # 画出墙的边框
    wall_turtle.color("#666")
    wall_turtle.pensize(3)
    wall_turtle.penup()
    wall_turtle.goto(x, y)
    wall_turtle.pendown()
    for i in range(2):
        wall_turtle.forward(width)
        wall_turtle.left(90)
        wall_turtle.forward(height)
        wall_turtle.left(90)
    wall_turtle.penup()


def go_up():
    """向上移动小乌龟"""
    y = player.ycor()
    y += 10
    # 判断小乌龟是否碰到墙
    if is_collision(player.xcor(), y):
        return
    player.sety(y)


def go_down():
    """向下移动小乌龟"""
    y = player.ycor()
    y -= 10
    # 判断小乌龟是否碰到墙
    if is_collision(player.xcor(), y):
        return
    player.sety(y)


def go_left():
    """向左移动小乌龟 """
    x = player.xcor()
    x -= 10
    if is_collision(x, player.ycor()):
        # 如果碰到墙，就不移动
        return
    player.setx(x)


def go_right():
    """向右移动小乌龟"""
    x = player.xcor()
    x += 10
    if is_collision(x, player.ycor()):
        return
    player.setx(x)


def is_collision(x, y):
    """判断小乌龟是否碰到墙"""
    for wall in walls:
        wall_x, wall_y, width, height = wall
        if wall_x - 5 <= x <= wall_x + width + 5 and wall_y - 5 <= y <= wall_y + height + 5:
            return True
    return False


wn.listen()
# 绑定键盘事件
# 当按下上箭头键时，调用go_up函数
wn.onkeypress(go_up, "Up")
# 当按下下箭头键时，调用go_down函数
wn.onkeypress(go_down, "Down")
# 当按下左箭头键时，调用go_left函数
wn.onkeypress(go_left, "Left")
# 当按下右箭头键时，调用go_right函数
wn.onkeypress(go_right, "Right")

turtle.done()
t1 = time.time()
time_interval = t1 - t0
print(f"耗时：{time_interval:.2f}秒，注意休息")
print("程序结束")
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))